package cn.fudong.bos.service.transit.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.fudong.bos.dao.take_delivery.WayBillRepository;
import cn.fudong.bos.dao.transit.TransitInfoRepository;
import cn.fudong.bos.domain.take_delivery.WayBill;
import cn.fudong.bos.domain.tranist.TransitInfo;
import cn.fudong.bos.service.transit.TransitInfoService;
import cn.fudong.bos.index.WayBillIndexRepository;

@Service
@Transactional
public class TransitInfoServiceImpl implements TransitInfoService {

	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;

	@Autowired
	private WayBillRepository waybillrepository;

	@Autowired
	private TransitInfoRepository transitinforepository;

	@Override
	public void createTransitions(String wayBillIds) {
		if (StringUtils.isNoneBlank(wayBillIds)) {
			for (String wayBillId : wayBillIds.split(",")) {
				WayBill wayBill = waybillrepository.findOne(Integer.parseInt(wayBillId));
				// 如果运单配送状态是1,待配送,保存运输信息
				if (wayBill.getSignStatus() == 1) {
					TransitInfo transitInfo = new TransitInfo();
					transitInfo.setStatus("出入库中转");// 出入库中转
					transitInfo.setWayBill(wayBill);// 添加信息
					transitinforepository.save(transitInfo);
					// 更改运单状态派送中
					wayBill.setSignStatus(2);
					// 同步索引库
					wayBillIndexRepository.save(wayBill);
				}

			}

		}

	}
	//分页查询
	@Override
	public Page<TransitInfo> findAll(Pageable pageable) {
		
		return transitinforepository.findAll(pageable);
	}

}
